package com.chagee.store.operation.server.controller.pc;

import com.alibaba.fastjson.JSONObject;
import com.chagee.base.utils.HttpResponse;
import com.chagee.base.utils.HttpResponseUtil;
import com.chagee.base.utils.PaginationData;
import com.chagee.base.utils.PaginationRequest;
import com.chagee.store.operation.common.exception.BizErrorCode;
import com.chagee.store.operation.common.utils.CommonUtil;
import com.chagee.store.operation.server.facade.CommentConfigurationFacade;
import com.chagee.store.operation.server.model.param.AddCommentConfigParam;
import com.chagee.store.operation.server.model.param.CommentConfigurationParam;
import com.chagee.store.operation.server.model.param.CommentDetailParam;
import com.chagee.store.operation.server.model.param.EnableCommentParam;
import com.chagee.store.operation.server.model.param.QueryOperateRecordParam;
import com.chagee.store.operation.server.model.param.UpdateCommentConfigParam;
import com.chagee.store.operation.server.model.vo.CommentConfigurationVO;
import com.chagee.store.operation.server.model.vo.OperateRecordVo;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * 评价配置
 *
 * @Author : hk
 * @Date : 2024/7/5
 */

@Slf4j
@RestController
@RequestMapping("/commentConfig")
public class CommentConfigurationController {

	@Resource
	private CommentConfigurationFacade commentConfigurationFacade;

	/**
	 * 分页查询评价配置
	 */
	@PostMapping("/query")
	@ApiOperation("评价配置分页查询")
	public HttpResponse<PaginationData<CommentConfigurationVO>> queryCommentConfigPage(@RequestBody PaginationRequest<CommentConfigurationParam> request) {
		log.info("#CommentConfigurationController.queryCommentConfigPage#request{}", JSONObject.toJSONString(request));
		return HttpResponseUtil.success(commentConfigurationFacade.queryCommentConfigPage(request));
	}

	/**
	 * 新建评价配置
	 */
	@PostMapping("/addComment")
	@ApiOperation("新建评价配置")
	public HttpResponse<Boolean> addComment(@RequestBody @Valid AddCommentConfigParam request) {
		log.info("#CommentConfigurationController.addEvaluate#request{}", JSONObject.toJSONString(request));
		if (StringUtils.isNotBlank(CommonUtil.validate(request))) {
			return HttpResponseUtil.build(null, BizErrorCode.REQUEST_VALID_FAILED.getErrorCode(), CommonUtil.validate(request));
		}
		return HttpResponseUtil.success(commentConfigurationFacade.addComment(request));
	}

	/**
	 * 编辑回显数据接口
	 */
	@PostMapping("/commentDetail")
	@ApiOperation("评价配置详情接口")
	public HttpResponse<CommentConfigurationVO> commentDetail(@RequestBody @Valid CommentDetailParam request) {
		log.info("#CommentConfigurationController.commentDetail#request{}", JSONObject.toJSONString(request));
		if (StringUtils.isNotBlank(CommonUtil.validate(request))) {
			return HttpResponseUtil.build(null, BizErrorCode.REQUEST_VALID_FAILED.getErrorCode(), CommonUtil.validate(request));
		}
		return HttpResponseUtil.success(commentConfigurationFacade.commentDetail(request));
	}

	/**
	 * 修改评价配置
	 */
	@PostMapping("/updateComment")
	@ApiOperation("修改评价配置")
	public HttpResponse<Boolean> updateComment(@RequestBody @Valid UpdateCommentConfigParam request) {
		log.info("#CommentConfigurationController.updateComment#request{}", JSONObject.toJSONString(request));
		if (StringUtils.isNotBlank(CommonUtil.validate(request))) {
			return HttpResponseUtil.build(null, BizErrorCode.REQUEST_VALID_FAILED.getErrorCode(), CommonUtil.validate(request));
		}
		return HttpResponseUtil.success(commentConfigurationFacade.updateComment(request));
	}

	/**
	 * 启用/禁用评价
	 */
	@PostMapping("/enableComment")
	@ApiOperation("启用/禁用评价")
	public HttpResponse<Boolean> enableComment(@RequestBody @Valid EnableCommentParam request) {
		log.info("#CommentConfigurationController.enableComment#request{}", JSONObject.toJSONString(request));
		if (StringUtils.isNotBlank(CommonUtil.validate(request))) {
			return HttpResponseUtil.build(null, BizErrorCode.REQUEST_VALID_FAILED.getErrorCode(), CommonUtil.validate(request));
		}
		return HttpResponseUtil.success(commentConfigurationFacade.enableComment(request));
	}

	/**
	 * 查询操作记录 recordType: 3
	 */
	@PostMapping("/queryOperateRecordPage")
	@ApiOperation("查询操作记录")
	public HttpResponse<PaginationData<OperateRecordVo>> queryOperateRecordPage(@RequestBody @Valid  PaginationRequest<QueryOperateRecordParam> param) {
		log.info("#CommentConfigurationController.queryOperateRecordPage#request{}", JSONObject.toJSONString(param));
		if (StringUtils.isNotBlank(CommonUtil.validate(param))) {
			return HttpResponseUtil.build(null, BizErrorCode.REQUEST_VALID_FAILED.getErrorCode(), CommonUtil.validate(param));
		}
		if (Objects.isNull(param.getPageNum()) || Objects.equals(0, param.getPageNum())) {
			param.setPageNum(1);
		}
		if (Objects.isNull(param.getPageSize()) || Objects.equals(0, param.getPageSize())) {
			param.setPageSize(10);
		}
		return HttpResponseUtil.success(commentConfigurationFacade.queryOperateRecordPage(param));
	}

}
